package com.aceitunaproject.server.entity;

import java.io.Serializable;

import com.aceitunaproject.server.player.Player;
import com.sun.sgs.app.*;


/**
 * Represents the trust or esteem that exists between an entity and a player
 * @author Anthony Peterson (MindWanderer)
 *
 * We can set a max and min or simply use comparisons without concern for exact values.
 * We can also use integers instead but real numbers allow for finer tweaking of 
 * gains and losses (whereas with integers change must come in units of 1 or more)
 */
public class Loyalty implements ManagedObject, Serializable, Comparable<Loyalty> {
	
	/**
	 * Serial UID for serialization purposes
	 * 64 bits from http://www.famkruithof.net/uuid/uuidgen
	 */
	private static final long serialVersionUID = 0xad8b0800200c9a66L;
	


	private ManagedReference<Player> playerRef;
	
	/** 
	 * Nominally 0 is neutrality, - is hate, + is loyalty
	 */
	private double score;
	
	/**
	 * Default score constructor
	 * Initializes loyalty to 0
	 * @param player a Player who this Entity has a loyalty score towards
	 */
	public Loyalty(Player player) {
		this.playerRef = AppContext.getDataManager().createReference(player);
		setScore(0.0);
	}

	/**
	 * Set the loyalty relationship sore
	 * @param score a double where higher numbers represent greater loyalty
	 */
	public void setScore(double score) {
		this.score = score;
	}

	/**
	 * Get the score of this loyalty
	 * @return the loyalty's score
	 */
	public double getScore() {
		return score;
	}

	
	
	
	/**
	 * Find out who the entity is loyal to
	 * @return who the entity is loyal to
	 */
	public Player getPlayer() {
		return playerRef.get();
	}
	
	/**
	 * Compare to the specified Loyalty.
	 * comparisons are made on the basis of score ordering
	 * @param o the Loyalty being compared to
	 */
	public int compareTo(Loyalty o)
	{
		if(score < o.score)
			return -1;
		else if(score == o.score)
			return 0;
		// Implies score > o.score when control falls to this case
		else
			return 1;
	}
}